// eslint-disable-next-line import/no-commonjs
const parseString = require('xml2js').parseString;
const md5 = require('js-md5');



// 判断对象类型
function typeObj(obj) {
  const type = Object.prototype.toString.call(obj);
  if (type === '[object Array]') {
    return 'Array';
  } else if (type === '[object Object]') {
    return 'Object';
  } else {
    return 'obj is not object or array';
  }
}
// 递归的结果
let pathArray = [];
//递归查找节点
/**
 *
 * @param {*} nextAllPath
 * @param {*} nextComponentPath
 * @param {*} xmlObject
 * @param {*} type => 'tagName','class','id','text'
 * @param {*} typeName
 */
const findPath = (
  nextAllPath = [],
  nextComponentPath = [],
  xmlObject,
  type,
  typeName,
) => {
  const xmlObjectType = typeObj(xmlObject);

  if (xmlObjectType === 'Array') {
    xmlObject.forEach(element => {
      findPath(nextAllPath, nextComponentPath, element, type, typeName);
    });
  }
  if (xmlObjectType === 'Object') {
    for (const key in xmlObject) {
      let needPush = false;
      const pathObject = {
        id: md5.base64([].concat(nextAllPath).toString() + JSON.stringify(xmlObject._)),
        allPath: [].concat(nextAllPath),
        componentPath: [].concat(nextComponentPath),
      };
      if (
        key === '_' && type === 'text' && xmlObject[key].indexOf(typeName) !== -1
      ) {
        // Text
        needPush = true;
        pathObject['className'] = xmlObject && xmlObject['$'] && xmlObject['$']['class'] ? xmlObject['$']['class'] : '';
        pathObject.content = xmlObject[key];
      } else if (key === typeName && type === 'tagName') {
        // tagName
        needPush = true;
        pathObject['className'] = xmlObject && xmlObject[key] && xmlObject[key][0] && xmlObject[key][0]['$'] && xmlObject[key][0]['$'] && xmlObject[key][0]['$']['class'] ? xmlObject[key][0]['$']['class'] : '';
      } else if (type === 'class' && key === '$' && xmlObject[key]['class'] && xmlObject[key]['class'].indexOf(typeName) !== -1) {
        // class
        needPush = true;
        pathObject['className'] = xmlObject && xmlObject['$'] && xmlObject['$']['class'] ? xmlObject['$']['class'] : '';
      } else if (type === 'id' && key === '$' && xmlObject[key]['id'] && xmlObject[key]['id'].indexOf(typeName) !== -1) {
        // id
        needPush = true;
        pathObject['className'] = xmlObject && xmlObject['$'] && xmlObject['$']['class'] ? xmlObject['$']['class'] : '';
      }
      // tagName add self path
      if (needPush && key === typeName && type === 'tagName') {
        pathObject['allPath'].push(key);
        pathArray.push(pathObject);
      }
      else if (needPush) {
        pathArray.push(pathObject);
      }
      const newComponentPath =
        key.indexOf('-') !== -1
          ? [...nextComponentPath, key]
          : [...nextComponentPath];
      const newAllPath = [...nextAllPath, key];
      findPath(newAllPath, newComponentPath, xmlObject[key], type, typeName);
    }
  }
};
/**
 *
 * @param {*} wxml wxml 字符串
 * @param {*} type 'tagName','class','id','text'
 * @param {*} typeName type对应的字符串
 */
const findPathFromWxml = (wxml, type, typeName) => {
  pathArray = [];
  const getXmlString = wxml;
  let xmlObject = {};
  parseString(getXmlString, function (err, result) {
    if (!err) {
      xmlObject = result;
    }
  });
  console.log('xmlObject =>', xmlObject);
  findPath([], [], xmlObject, type, typeName);
  console.log('pathArray String=>', JSON.stringify(pathArray));
  return pathArray;
};
module.exports = findPathFromWxml;
